package io.lckgroup.modules.lck.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.lckgroup.common.utils.PageUtils;
import io.lckgroup.common.utils.Query;
import io.lckgroup.modules.lck.dao.ShopFoodAuditDao;
import io.lckgroup.modules.lck.dao.ShopFoodDao;
import io.lckgroup.modules.lck.dao.ShopFoodSpecDao;
import io.lckgroup.modules.lck.entity.ShopFoodAuditEntity;
import io.lckgroup.modules.lck.entity.ShopFoodEntity;
import io.lckgroup.modules.lck.entity.ShopFoodSpecEntity;
import io.lckgroup.modules.lck.form.ShopFoodOrMerchantAduit;
import io.lckgroup.modules.lck.service.ShopFoodAuditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("shopFoodAuditService")
public class ShopFoodAuditServiceImpl extends ServiceImpl<ShopFoodAuditDao, ShopFoodAuditEntity> implements ShopFoodAuditService {
    //注入属性
    @Value("${backend.innerImg}")
    private String innerImg;
    //审核表
    @Autowired
    private ShopFoodAuditDao shopFoodAuditDao;
    //菜品表
    @Autowired
    private ShopFoodDao shopFoodDao;

    @Autowired
    private ShopFoodSpecDao shopFoodSpecDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ShopFoodAuditEntity> page = this.page(
                new Query<ShopFoodAuditEntity>().getPage(params),
                new QueryWrapper<ShopFoodAuditEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * @param params
     * @return 查询审核的数据
     */
    @Override
    @Transactional
    @Cacheable(value = "selectPageByAuditState", key = "#params.get(\"redis\")") //存方法返回值
    public IPage<Map<String, Object>> selectPageByAuditState(@RequestParam Map<String, Object> params) {
        // TODO Auto-generated method stub
        // 不进行 count sql 优化，解决 MP 无法自动优化 SQL 问题，这时候你需要自己查询 count 部分
        // page.setOptimizeCountSql(false);
        // 当 total 为小于 0 或者设置 setSearchCount(false) 分页插件不会进行 count 查询
        // 要点!! 分页返回的对象与传入的对象是同一个

        String merchantName = params.get("key").toString();
        String auditState = "";
        if (params.containsKey("state")) {
            auditState = params.get("state").toString();
        }
        ShopFoodOrMerchantAduit shopFoodOrMerchantAduit = new ShopFoodOrMerchantAduit();
        if (("").equals(merchantName)) {
            shopFoodOrMerchantAduit.setMerchantName("");
        } else {
            shopFoodOrMerchantAduit.setMerchantName(merchantName);
        }
        if (("").equals(auditState)) {
            shopFoodOrMerchantAduit.setAuditState(3);
        } else {
            shopFoodOrMerchantAduit.setAuditState(Integer.valueOf(auditState));
        }
        Page<Map<String, Object>> page = new Page<>(Integer.parseInt(params.get("page").toString()), Integer.valueOf(params.get("limit").toString()));
        IPage<Map<String, Object>> map = shopFoodAuditDao.selectAll(page, shopFoodOrMerchantAduit.getMerchantName(), shopFoodOrMerchantAduit.getAuditState());
        for (int i = 0; i < map.getRecords().size(); i++) {
           String imgUrl;
            if(map.getRecords().get(i).get("img_url")!=null){
                //替换本地IP
                imgUrl=map.getRecords().get(i).get("img_url").toString();
                int index=imgUrl.indexOf("/dc");
                imgUrl=innerImg+imgUrl.substring(index,imgUrl.length());
                map.getRecords().get(i).put("img_url",imgUrl);
            }
            List<ShopFoodSpecEntity> specList;
            //如果有规格
            if ((Boolean) map.getRecords().get(i).get("is_specs")) {
                QueryWrapper queryWrapper = new QueryWrapper();
                ShopFoodSpecEntity shopFoodSpecEntity = new ShopFoodSpecEntity();
                shopFoodSpecEntity.setGoodsId(Long.parseLong(map.getRecords().get(i).get("food_id").toString()));
                queryWrapper.setEntity(shopFoodSpecEntity);
                specList = shopFoodSpecDao.selectList(queryWrapper);
                map.getRecords().get(i).put("specList", specList);
            }

        }
        return map;
    }

    /**
     * @param params 审核菜品
     */
    @Override
    @Transactional
    @CacheEvict(value = {"findFoodList", "selectPageByAuditState", "findFoodList", "selectPageByAuditState"}, allEntries = true)
    public void AuditFood(@RequestParam Map<String, Object> params) {
        // 修改 shop_food_audit
        Long foodIds = Long.parseLong(params.get("foodId").toString());
        int auditState = Integer.parseInt(params.get("auditState").toString());
        int isUpper = Integer.parseInt(params.get("isUpper").toString());
//        System.out.println("isUpper:" + isUpper);
        String auditInfo = params.get("aduitInfo").toString();
        //修改ShopFoodAuditEntity中的auditState
        ShopFoodAuditEntity shopFoodAuditEntity = new ShopFoodAuditEntity();
        shopFoodAuditEntity.setGmtModified(new Date());
        shopFoodAuditEntity.setFoodId(foodIds);
        shopFoodAuditEntity.setAuditState(auditState);
        if (auditInfo.equals("")) {
            shopFoodAuditEntity.setAuditInfo("未知原因");
        } else {
            shopFoodAuditEntity.setAuditInfo(auditInfo);
        }
        shopFoodAuditDao.AuditShopFoodAudit(shopFoodAuditEntity);
        // 修改 of_shop_food
        ShopFoodEntity shopFoodEntity = new ShopFoodEntity();
        shopFoodEntity.setId(foodIds);
        shopFoodEntity.setIsUpper(isUpper);
        shopFoodEntity.setGmtModified(new Date());
        shopFoodDao.AuditShopFood(shopFoodEntity);
    }

    /**
     * @param ids 批量审核
     */
    @Override
    @Transactional
    @CacheEvict(value = {"findFoodList", "selectPageByAuditState", "findFoodList", "selectPageByAuditState"}, allEntries = true)
    public void BatchAudit(@RequestBody Long[] ids) {
        shopFoodAuditDao.PassAuditBatchShopFoodAudit(ids);
        shopFoodDao.PassAuditBatchShopFood(ids);
    }
}